package cn.bqjr.eily.web.exception.handler;

import cn.bqjr.eily.core.annotation.DisableAutoLog;
import cn.bqjr.eily.web.entity.BaseResponseVO;
import cn.bqjr.eily.web.exception.resolver.IExceptionResolver;
import cn.bqjr.eily.web.utils.RUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 异常统一处理
 * Created by heshan on 2017/5/7.
 */
@DisableAutoLog
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class DefaultExceptionHandler implements ErrorController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IExceptionResolver resolver;

    @RequestMapping
    @ResponseBody
    public ResponseEntity<BaseResponseVO> errorRespByJSON() {
        return RUtils.error(resolver.resolve(request));
    }

    @Override
    public String getErrorPath() {
        return "";
    }
}
